enable disable all agents within a scenario (#1506)

* tentative work on enable disable all agents within a scenario

* more work with pr

* more work

* better names and save a line of code

* coffeescript to change modal text and hidden disabled value

* redoing coffeescript class to save some code

* initing the function

* updated coffeescript

* change text from specific modal, made names more specific

* updates disabled test and rubocop bug

* improved test and refactored method per rubocop warnings

* switched from 0 to false

* switched from 0 to false and 1 to true

* fixed null bug in postgresql database

* fixed ruby boolean bug

* moved instances into let lazy load and use proper agent fixture

* putting in a different fix

* using existing membership and agent and another version of test

* switch back to 2 not 3 after rm an agent in yml

* made test passed locally

* forgot to remove debuging pp

* small spacing bug

* trying to get rid of newline in file

* typo not agent but scenario

* minor changes to syntax and naming

* rm potential failure and changed update_all statement

* fixing new line issue

* removed unnecessary if else statement from method

Judy Ngai 8 anos atrás
pai
commit
f57a3af521

+ 15 - 0
app/assets/javascripts/pages/scenario-show-page.js.coffee

@@ -0,0 +1,15 @@
1
+class @ScenarioShowPage
2
+  constructor:() ->
3
+    @changeModalText()
4
+
5
+  changeModalText: () ->
6
+    $('#disable-all').click ->
7
+      $('#enable-disable-agents .modal-body').text 'Would you like to disable all agents?'
8
+      $('#scenario-disabled-value').val 'true'
9
+    $('#enable-all').click ->
10
+      $('#enable-disable-agents .modal-body').text 'Would you like to enable all agents?'
11
+      $('#scenario-disabled-value').val 'false'
12
+
13
+$ ->
14
+  Utils.registerPage(ScenarioShowPage, forPathsMatching: /^scenarios/)
15
+

+ 10 - 0
app/controllers/scenarios_controller.rb

@@ -95,6 +95,16 @@ class ScenariosController < ApplicationController
95 95
     end
96 96
   end
97 97
 
98
+  def enable_or_disable_all_agents
99
+    @scenario = current_user.scenarios.find(params[:id])
100
+
101
+    @scenario.agents.update_all(disabled: params[:scenario][:disabled] == 'true')
102
+    respond_to do |format|
103
+      format.html { redirect_to @scenario, notice: 'The agents in this scenario have been successfully updated.' }
104
+      format.json { head :no_content }
105
+    end
106
+  end
107
+
98 108
   def destroy
99 109
     @scenario = current_user.scenarios.find(params[:id])
100 110
     @scenario.destroy_with_mode(params[:mode])

+ 19 - 0
app/views/scenarios/_enable_agents_modal.html.erb

@@ -0,0 +1,19 @@
1
+<div id="enable-disable-agents" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="enableAgentLabel" aria-hidden="true">
2
+  <div class="modal-dialog modal-sm">
3
+    <div class="modal-content">
4
+      <div class="modal-header">
5
+        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
6
+        <h4 class="modal-title">Confirm</h4>
7
+      </div>
8
+      <div class="modal-body">
9
+      </div>
10
+      <div class="modal-footer">
11
+        <%= form_for(scenario, as: :scenario, url: enable_or_disable_all_agents_scenario_path(scenario), method: 'PUT') do |f| %>
12
+          <%= f.hidden_field :disabled, value: '', id: "scenario-disabled-value" %>
13
+          <%= f.button 'No', class: 'btn btn-default', 'data-dismiss' => 'modal' %>
14
+          <%= f.submit 'Yes', class: 'btn btn-primary' %>
15
+        <% end %>
16
+      </div>
17
+    </div>
18
+  </div>
19
+</div>

+ 3 - 0
app/views/scenarios/show.html.erb

@@ -24,8 +24,11 @@
24 24
         <% end %>
25 25
         <%= link_to icon_tag('glyphicon-share-alt') + ' Share', share_scenario_path(@scenario), class: "btn btn-default" %>
26 26
         <%= link_to icon_tag('glyphicon-trash') + ' Delete', '#', data: { toggle: 'modal', target: "#confirm-scenario-deletion-#{@scenario.id}"}, class: "btn btn-default" %>
27
+        <%= link_to icon_tag('glyphicon-play') + 'Enable all Agents', '#', data: { toggle: 'modal', target: "#enable-disable-agents"}, class: "btn btn-default", id: "enable-all" %>
28
+        <%= link_to icon_tag('glyphicon-pause') + 'Disable all Agents', '#', data: { toggle: 'modal', target: "#enable-disable-agents"}, class: "btn btn-default", id: "disable-all" %>
27 29
       </div>
28 30
     </div>
29 31
   </div>
30 32
 </div>
31 33
 <%= render 'scenarios/confirm_deletion_modal', scenario: @scenario %>
34
+<%= render 'scenarios/enable_agents_modal', scenario: @scenario %>

+ 1 - 0
config/routes.rb

@@ -50,6 +50,7 @@ Huginn::Application.routes.draw do
50 50
     member do
51 51
       get :share
52 52
       get :export
53
+      put :enable_or_disable_all_agents
53 54
     end
54 55
 
55 56
     resource :diagram, :only => [:show]

+ 9 - 0
spec/controllers/scenarios_controller_spec.rb

@@ -140,6 +140,15 @@ describe ScenariosController do
140 140
     end
141 141
   end
142 142
 
143
+  describe 'PUT enable_or_disable_all_agents' do
144
+    it 'updates disabled on all agents in a scenario for the current user' do
145
+      @params = {"scenario"=>{"disabled"=>"true"}, "commit"=>"Yes", "id"=> scenarios(:bob_weather).id}
146
+      put :enable_or_disable_all_agents, @params
147
+      expect(agents(:bob_rain_notifier_agent).disabled).to eq(true)
148
+      expect(response).to redirect_to(scenario_path(scenarios(:bob_weather)))
149
+    end
150
+  end
151
+
143 152
   describe "DELETE destroy" do
144 153
     it "destroys only Scenarios owned by the current user" do
145 154
       expect {